package com.by.facade.enums.information.sub;

import com.by.facade.enums.information.PromotionPlatformEventTypeEnum;
import com.by.facade.enums.information.PromotionPlatformTypeEnum;
import com.by.toolkit.utils.UrlUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.Map;

import static com.by.toolkit.utils.ExceptionUtil.throwError;

/**
 * @author Tian Wei
 * @version 1.0
 * @date 2024/3/1 11:43 AM
 */
@Getter
@AllArgsConstructor
public enum KuaiShouPromotionPlatformEventEnum {

    FORM(9, "表单", PromotionPlatformEventTypeEnum.FORM.getType()),
    WX_CP_FRIEND_OPERATION(118, "完成加V", PromotionPlatformEventTypeEnum.WX_CP_FRIEND_OPERATION.getType()),
    ;
    private final Integer kuaiShouEventType;
    private final String desc;
    private final Integer promotionPlatformEventType;
    public static final String BASE_PUSH_URL = "http://ad.partner.gifshow.com/track/activate";
    public static final String OK = "OK";
    public static KuaiShouPromotionPlatformEventEnum resolveByPromotionPlatformEventType(Integer promotionPlatformEventType) {
        return (KuaiShouPromotionPlatformEventEnum) PromotionPlatformTypeEnum.PROMOTION_PLATFORM_EVENT_TYPE_MAP.getOrDefault(PromotionPlatformTypeEnum.KUAI_SHOU.getType(), Collections.emptyMap()).get(promotionPlatformEventType);
    }

    public String generatePushUrl(String url) {

        String callbackCode = UrlUtil.getUrlParam(url).get("callback");
        if (callbackCode == null || callbackCode == "") {
            throwError("快手平台事件回调推送失败,");
        }
        Map<String, Object> params = Map.of(
                "event_type", this.kuaiShouEventType,
                "event_time", System.currentTimeMillis(),
                "callback", callbackCode
        );
        return UrlUtil.httpUrlAppendParam(BASE_PUSH_URL, params);
    }

    public static Boolean checkResult(Map<String,Object> resultMap){
        return resultMap.values().contains(OK);
    }

}
